package engine.control
{
	import engine.data.ModelLocator;
	
	import flash.display.Stage;
	import flash.events.MouseEvent;
	
	import newx.StageInfo;
	import newx.com.control.Scroll.NxScrollBar;
	import newx.com.control.Scroll.NxScrollEvent;
	import newx.com.control.Scroll.NxScrollSkin;

	public class MainSpaceControl
	{
		
		private var _stage : Stage;
		
		//纵向
		private var _verticalBar : NxScrollBar;
		
		//横向
		private var _horizontalBar : NxScrollBar;
		
		private var _maxWidth : int = 1000;
		
		private var _maxHeight : int = 600;
		
		private var _barWidth	:int	=	13;
		
		//可视区域x坐标
		private var _xPoint : int;
		
		//可视区域y坐标
		private var _yPoint : int;
		
		public function MainSpaceControl()
		{
			init();
		}
		
		private static var _self			:MainSpaceControl;
		public static function getInstance():MainSpaceControl
		{
			if ( _self == null )
			{
				_self = new MainSpaceControl();
			}
			return _self;
		}
		
		private function init() : void
		{
			_stage = StageInfo.stage;
			
			//监听舞台大小改变
			_stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			
			_verticalBar = new NxScrollBar(NxScrollBar.VERTICAL, 0, _maxHeight, 10, new NxScrollSkin("vScrol_1","vScrol_2"));
			_horizontalBar = new NxScrollBar(NxScrollBar.HORIZONTAL, 0, _maxWidth, 10, new NxScrollSkin("vScrol_1","vScrol_2"));
		
			_verticalBar.addEventListener(NxScrollEvent.UPDATE_AREA, onCheckBarV);
			_horizontalBar.addEventListener(NxScrollEvent.UPDATE_AREA, onCheckBarH);
			
//			_stage.addChild(_verticalBar);
//			_stage.addChild(_horizontalBar);
			
			onResize();
		}
		
		/**
		 *	界面改变，根据需求显示滚动条 
		 * @param evt
		 * 
		 */
		public function onResize():void
		{
			var width : int = _stage.stageWidth;
			var height : int = _stage.stageHeight;
			
			_maxWidth = int(
				ModelLocator.getInstance().mainIni.getContent("stageConfig","width")
			);
			_maxHeight = int(
				ModelLocator.getInstance().mainIni.getContent("stageConfig","height") 
			);
			
			if(width < _maxWidth)
			{
				_maxHeight	=	_maxHeight + _barWidth;
			}
			
			if(height < _maxHeight)
			{
				_maxWidth	=	_maxWidth + _barWidth;
			}
			
			//更新滚动条(32 是左右2点击块大小)
			_horizontalBar.update(width, (width  - 2 * _barWidth) / _maxWidth * width);
			_verticalBar.update(height - _barWidth, (height - 2 * _barWidth - _barWidth) / _maxHeight * height );
			
			//滚动条位置更新
			_horizontalBar.y = height - _barWidth;
			_verticalBar.x = width - _barWidth;
			
			//是否显示滚动条
			_horizontalBar.visible = width < _maxWidth;
			_verticalBar.visible = height < _maxHeight;
			
//			onCheckBarH();
//			onCheckBarV();
			
			_horizontalBar.scroll = 0;
			_verticalBar.scroll = 0;
//			
//			StageInfo.mainSpace.x = _horizontalBar.visible ? 0 : (width - _maxWidth) / 2;
//			StageInfo.mainSpace.y = _verticalBar.visible ? 0 : (height - _maxHeight) / 2;
		}
		
		private function onCheckBarH(evt : NxScrollEvent = null):void
		{
			if(!_horizontalBar.visible ) 
			{
				return;
			}
			
			var width : int = _stage.stageWidth;
			var height : int = _stage.stageHeight;
			
			if(width > StageInfo.rootW && height > StageInfo.rootH)
			{
				return;
			}
			
			
			if(width < StageInfo.rootW)
			{
				if(evt == null)
				{
				}else if(evt.target == _horizontalBar && _horizontalBar.visible)
				{
					var tcX : int =	_horizontalBar.barBtn.x - _barWidth;
					_xPoint =  tcX * (_maxWidth / (width - 2 * _barWidth));
				}
			}
			
			StageInfo.mainSpace.x = - _xPoint;
		}
		
		private function onCheckBarV(evt : NxScrollEvent = null):void
		{
			if(!_verticalBar.visible ) 
			{
				return;
			}
			
			var width : int = _stage.stageWidth;
			var height : int = _stage.stageHeight;
			
			if(width > StageInfo.rootW && height > StageInfo.rootH)
			{
				return;
			}
			
			if(height < StageInfo.rootH)
			{
				if(evt == null)
				{
				}else if(evt.target == _verticalBar && _verticalBar.visible){
					var tcY : int =	_verticalBar.barBtn.y - _barWidth;
					_yPoint = tcY * (_maxHeight / (height - 3 * _barWidth));
				}
			}
			
			StageInfo.mainSpace.y = - _yPoint;
		}
		
		private function onMouseWheel(e:MouseEvent):void
		{
			_verticalBar.onMouseWheel(e);
		}

	}
}